 /**
 * 
 */
package com.security.frame.token.social;

 import com.security.frame.core.code.sms.mobile.SmsCodeAuthenticationToken;
 import com.security.frame.core.common.CommonConstants;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.security.authentication.AuthenticationServiceException;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.AuthenticationException;
 import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
 import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 import org.springframework.util.Assert;

 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 /**
  * 自定义openid登陆过滤器
  *
  * @author wangjie
  * @version V1.0
  * @date 2019/11/6
  */
@Slf4j
public class OpenIdAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

	 private String openIdParametr = CommonConstants.DEFAULT_PARAMETER_NAME_OPENID;
	 private String providrtIdParametr = CommonConstants.DEFAULT_PARAMETER_NAME_PROVIDERID;
	 private boolean postOnly = true;

	public OpenIdAuthenticationFilter() {
		super(new AntPathRequestMatcher(CommonConstants.DEFAULT_LOGIN_PROCESSING_URL_OPENID, "POST"));
	}


	@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
			throws AuthenticationException {
		if (postOnly && !request.getMethod().equals("POST")) {
			log.error("【Authentication method not supported: {}】",request.getMethod());
			throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
		}

		String openId = obtainOpenIde(request);
		String providerId = obtainProviderIde(request);


		if (null == openId) {
			openId = "";
		}
		if (null == providerId) {
			providerId = "";
		}

		openId = openId.trim();
		providerId = providerId.trim();

		OpenIdAuthenticationToken authRequest = new OpenIdAuthenticationToken(openId,providerId);

		// Allow subclasses to set the "details" property
		setDetails(request, authRequest);

		return this.getAuthenticationManager().authenticate(authRequest);
	}


	 /**
	  * 获取openid
	  */
	 protected String obtainOpenIde(HttpServletRequest request) {
		 return request.getParameter(openIdParametr);
	 }

	 /**
	  * 获取providerIde
	  */
	 protected String obtainProviderIde(HttpServletRequest request) {
		 return request.getParameter(providrtIdParametr);
	 }


	protected void setDetails(HttpServletRequest request, OpenIdAuthenticationToken authRequest) {
		authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
	}


	public void setMobileParameter(String usernameParameter) {
		Assert.hasText(usernameParameter, "Username parameter must not be empty or null");
		this.openIdParametr = usernameParameter;
	}

	 public void setPasswordParameter(String passwordParameter) {
		 Assert.hasText(passwordParameter, "Password parameter must not be empty or null");
		 this.providrtIdParametr = passwordParameter;
	 }
	public void setPostOnly(boolean postOnly) {
		this.postOnly = postOnly;
	}

	public final String getMobileParameter() {
		return openIdParametr;
	}

	 public final String getPasswordParameter() {
		 return providrtIdParametr;
	 }

}
